package com.hyh.baowenlioaworking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.baowenlioaworking.commom.Constants;
import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.model.Position;
import com.hyh.baowenlioaworking.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.controller
 * @ClassName PositionController
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  14:08
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@RestController
@RequestMapping("/pos")
public class PositionController {
    @Autowired
    public PositionService positionService;

    /**
     * 查询所有信息
     *
     * @return
     */
    @GetMapping("/getall")
    public List<Position> findAll() {
        return positionService.findAll();
    }
    @GetMapping("{page}/{size}")
    public R findAll(@PathVariable Integer page,@PathVariable Integer size){
        Page<Position> pagePosition = new Page<>(page, size);
        IPage<Position> positionIPage = positionService.findAll(pagePosition);
        return R.success("200",positionIPage);
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Position findById(@PathVariable Integer id){
        return  positionService.findById(id);
    }

    /**
     * 删除数据，根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R<Integer> deleteById(@PathVariable int id) {
        return positionService.deleteById(id);
    }

    /**
     * 修改数据，根据id修改id下的数据
     *
     * @param position
     * @return
     */
    @PostMapping("/update")
    public R<String> updatePosition(@RequestBody Position position) {
        positionService.updatePosition(position);
        return R.success(Constants.CODE_200,"修改成功");
    }

    /**
     * 添加数据
     *
     * @param position
     * @return
     */
    @PutMapping("/add")
    public R<String> addPosition(@RequestBody Position position) {
        return positionService.addPosition(position);
    }

    /**
     * 获得数据的总数
     * @return
     */
    @GetMapping("/getnum")
    public int getTotal() {
        return positionService.getTotal();
    }
}
